//
//  ATWUtil.m
//  WorkplusIM-IOS
//
//  Created by lingen on 15/10/8.
//  Copyright © 2015年 lingen.liu. All rights reserved.
//

#import "ATWUtil.h"
#import <AdSupport/ASIdentifierManager.h>
#import <CommonCrypto/CommonCrypto.h>
#import "ATWImApplication.h"
#import <UIKit/UIKit.h>

@implementation ATWUtil

+ (NSString *)deviceId {
    return [ASIdentifierManager sharedManager].advertisingIdentifier.UUIDString;
}

+ (UInt64)dateOfCurrentTimestamp {
    return [[NSDate date] timeIntervalSince1970] * 1000;
}

//服务器时间
+ (UInt64)dateOfServerTimestamp {
    UInt64 timestamp = [[NSDate date] timeIntervalSince1970] * 1000;
    if ([ATWImApplication sharedInstance].messageTimeDifference) {
        return timestamp + [ATWImApplication sharedInstance].messageTimeDifference;
    }
    return timestamp;
}

#pragma md5计算
+ (NSData *)md5WithString:(NSString *)hashString {
    return [self md5WithData:[hashString dataUsingEncoding:NSUTF8StringEncoding]];
}

+ (NSData *)md5WithData:(NSData *)hashData {
    unsigned char *digest;
    digest = malloc(CC_MD5_DIGEST_LENGTH);
    CC_MD5(hashData.bytes, (CC_LONG)hashData.length, digest);
    NSData *result = [NSData dataWithBytes:digest length:CC_MD5_DIGEST_LENGTH];
    free(digest);
    return result;
}

+ (NSString *)base64WithData:(NSData *)hashData {
    return [[hashData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength] stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];
}

+ (NSString *)base64DigestUsingMd5WithString:(NSString *)hashString {
    return [self base64WithData:[self md5WithString:hashString]];
}

#pragma String util

+ (BOOL)isEmpty:(NSString *)aString {
    if (aString == nil || aString == NULL || [aString isEqual:[NSNull null]] || aString.length == 0) {
        return YES;
    }
    if ([aString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length ==0) {
        return YES;
    }
    return NO;
}

+ (BOOL)isNotEmpty:(NSString *)aString {
    return ![self isEmpty:aString];
}


@end
